// 推广
const basePage = require('../../common/BasePage')
Page({
  behaviors: [basePage],

  /**
   * 页面的初始数据
   */
  data: {
    img: "",
    userInfo: {},

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    setTimeout(() => {
      this.widget = this.selectComponent('.widget');
    }, 500);


    let userInfo = getApp().globalData.userInfo;
    let avatar = this.get_imgUrl(userInfo.avatar);
    setTimeout(() => {
      this.request({
        url: this.data.host + '/v1/users/promotion-code',

      }, res => {
        this.setData({
          img: this.get_imgUrl(res.data),
          userInfo: userInfo
        })
        let wxml = `<view class="container" >
        <image src="${this.data.href}/tj-img.png" class="bg-img" mode="aspectFit"></image>
        <image src="${this.data.imgHost+res.data}" class="code-img" mode="aspectFit"></image>
        <view class="info" >
          <text  class="name">${userInfo.nickName}</text>
          <text  class="desc">扫描二维码 领取新人券</text>
        </view>
        <image src="${avatar}" class="avatar" mode="aspectFit"></image>
      </view>
      样式`;
        let style = {
          container: {
            width: 319,
            height: 415,
            // backgroundColor: '#ff0000'
          },
          bgImg: {
            width: 319,
            height: 415,
          },
          codeImg: {
            left: 14,
            top: 16,
            width: 291,
            height: 291,
            position: "absolute"
          },
          info: {
            position: "absolute",
            left: 20,
            right: 20,
            top: 340,
            height: 40,
            // scale: 1.5
          },
          name: {
            // backgroundColor: '#0fff00',
            color: '#1A1A1A',
            // fontWeight: 'bold',
            width: 100,
            height: 30,
            fontSize: 19
          },
          desc: {
            color: '#5A5A5A',
            height: 30,
          },
          avatar: {
            position: "absolute",
            top: 340,
            right: 20,
            width: 50,
            height: 50,
            borderRadius: 25
          }
        };
        const p1 = this.widget.renderToCanvas({
          wxml,
          style
        })
        p1.then((res) => {
          console.log('container', res)
          this.container = res;
          // this.extraImage()
        }).catch(err => {
          console.log("p1 err", err);
        })
        // console.log('user', this.data.userInfo);
      })
    }, 1000);


  },
  extraImage() {
    const p2 = this.widget.canvasToTempFilePath();
    wx.showLoading({
      title: '保存图片',
      mask: true,

    })
    p2.then(res => {
      console.log('p2', res);
      if (res.tempFilePath) {
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: (res) => {
            wx.showToast({
              title: '保存成功',

            })
          },
          fail: (res) => {
            wx.showToast({
              title: '保存失败',

              icon: 'error',

            })
          },
          complete: (res) => {},
        })
      } else {
        wx.showToast({
          title: '生成失败',
          icon: 'error',

        })
      }

      // this.setData({
      //   img: res.tempFilePath,
      //   width: this.container.layoutBox.width,
      //   height: this.container.layoutBox.height
      // })
    })
  },
  jl_click() {
    wx.navigateTo({
      url: '/pages/usercenter/list/inviters',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },


})